<?php
class Movie extends CI_Controller{
    function __construct() {
        parent::__construct();
    }

    function index(){
        $this->_header(null, ''); //header
        $this->_slideShow(); //slider
        $this->_leftMenu(); //main menu
        
        $this->load->model('movie/m_movie');
        $this->load->helper('myurl');
        $this->load->helper('pagination');

        $content = array();
        $content['rateList'] = $this->m_movie->getMostRateMovies(0, 10);
        $content['mostViewsList'] = $this->m_movie->getMostReviewMovies(0, 10);
        $content['latestList'] = $this->m_movie->getLatestMovies(0, 10);

        $maxPage = $this->m_movie->maxPage(10);
        $content['endPage'] = $maxPage;
        $content['pageArr'] = pageArray(2, $maxPage, 1);
        $content['currPage'] = 1;
        $this->load->view('movie/v_home_content', $content);

        
        $this->_footer();//footer
    }

    /**
     *xem thong tin ve film nay
     * @param type $movieID id cua tap film nay
     */
    function view($movieID){
        //load model
        $this->load->model(array('movie/m_movie',
                                'movie/m_episode'
                                ));
        $content = array();
        //load thong tin bo phim
        $movieInfo = $this->m_movie->getMovieInfo($movieID);
        $content['movie_id'] = $movieID;
        $content['movie_name'] = $movieInfo['movie_name'];        
        $content['movie_image'] = $movieInfo['movie_image'];
        $content['movie_main_content'] = $movieInfo['movie_main_content'];
        $content['movie_introduction'] = $movieInfo['movie_introduction'];
        $content['movie_actors'] = $movieInfo['movie_actors'];
        $content['movie_rating'] = $movieInfo['movie_rating'];
        $content['movie_year'] = $movieInfo['movie_year'];
        $content['category_name'] = $movieInfo['category_name'];
        $content['episodeList'] = $this->m_movie->getEpisodeList($movieID);

        $content['differentMovies'] = $this->_differentMovies($movieID);
        //meta
        $meta = "<meta http-equiv=\"Content-Type\" content=\"" . $movieInfo['movie_name'] . "\">";
        $this->load->helper('myurl');
        //load header
        $title =  $movieInfo['movie_name'];
        $this->_header($title, $meta);
        $this->_leftMenu();
        $this->load->view('movie/v_info_content', $content);
        $this->_footer();
    }

    /**
     * play video
     * @param type $episodeID id cua tap film
     */
    function play($episodeID){
        $this->session->set_userdata('epID', $episodeID);
        //load view
        //load model
        $this->load->model(array('movie/m_movie',
                                  'movie/m_episode'));

        $content = array();
        $content['episodeID'] = $episodeID;
        //load thong tin bo phim
        $episodeInfo = $this->m_episode->getEpisodeInfo($episodeID);
        $content['movie_name'] = $episodeInfo['movie_name'];
        $content['episode_name'] = $episodeInfo['episode_name'];
        $content['episode_views'] = $episodeInfo['episode_views'];
        $content['movie_image'] = $episodeInfo['movie_image'];
        $content['movie_rating'] = $episodeInfo['movie_rating'];
        $content['movie_id'] = $episodeInfo['movie_id'];

        $content['differentMovies'] = $this->_differentMovies($episodeInfo['movie_id']);
        //load header
        $meta = "<meta http-equiv=\"Content-Type\" content=\"" . $episodeInfo['movie_name'] . ' - ' . $episodeInfo['episode_name'] . "\">";
        $title =  $episodeInfo['movie_name'] . ' - ' . $episodeInfo['episode_name'];
        
        
        //load danh sach cac tap film cua film nay
        $content['episodeList'] = $this->m_episode->getEpisodeIDList($episodeID);
        //tang luot xem len 1
        $this->m_episode->addView($episodeID, $episodeInfo['episode_views']);
        $this->load->helper('myurl');
        //load view
        $this->_header($title, $meta);
        $this->_leftMenu();
        $this->load->view('movie/v_play_content', $content);
        $this->_footer();
    }
    
    /**
     *lay danh sach cac url cua tap film nay
     * @param type $episodeID id cua tap file
     */
    function urlList(){
        if(!$this->session->userdata('epID')) return;
        $episodeID = $this->session->userdata('epID');
        $this->load->model('movie/m_episode');
        
        //chay ham lay danh sach cac url cua tap film nay        
        $pathList = $this->m_episode->getEpisodePathList($episodeID);
        
        $data = array();
        $data['urlList'] = explode(';', $pathList);

       //foreach($data['urlList'] as $item)
         //   echo $item . '<br>';
        $this->load->view('movie/v_playlist_xml', $data);
    }

    /**
     * cho diem bo film nay
     * @param  $movieID
     * @param  $vote
     * @return void
     */
    function starRating($movieID, $vote){
        $this->load->model('movie/m_movie');
        //lay ra so diem hien tai, so luot vote trong csdl
        //tinh ra diem trung binh
        //ghi lai csdl
        $result = $this->m_movie->getRating($movieID);
        if(!$result)
            //do nothing
            return ;
        else{
            //tinh lai diem tb va ghi vao csdl
            $rating = $result['rating']; //diem hien tai
            $total_votes = $result['total_votes'] + 1; //tong luot cho diem - tranh viec chia cho 0
            $temp = $rating * ($total_votes - 1) + $vote;
            $data = array(
                'rating' => $temp / $total_votes,
                'total_votes' => $total_votes
            );

            //update
            $this->m_movie->setRating($movieID, $data);
        }

    }

    /*
     * function for AJax here
     */


    function latestMovie($page){
        $this->load->model('movie/m_movie');
        $this->load->helper('myurl');
        $content = array();
        $content['latestList'] = $this->m_movie->getLatestMovies(($page - 1) * 10, 10);
        $this->load->helper('pagination');
        $maxPage = $this->m_movie->maxPage(10);
        $content['endPage'] = $maxPage;
        $content['pageArr'] = pageArray(2, $maxPage , $page);
        $content['currPage'] = $page;
        $this->load->view('movie/v_latestmovies', $content);
    }

    function topRate($page){
        $this->load->model('movie/m_movie');
        $this->load->helper('myurl');
        $content = array();
        $content['rateList'] = $this->m_movie->getMostRateMovies(($page - 1) * 10, 10);
        $this->load->helper('pagination');
        $maxPage = $this->m_movie->maxPage(10);
        $content['endPage'] = $maxPage;
        $content['pageArr'] = pageArray(2, $maxPage , $page);
        $content['currPage'] = $page;
        $this->load->view('movie/v_topmovies', $content);
    }

    function mostViews($page){
        $this->load->model('movie/m_movie');
        $this->load->helper('myurl');
        $content = array();
        $content['mostViewsList'] = $this->m_movie->getMostReviewMovies(($page - 1) * 10, 10);
        $this->load->helper('pagination');
        $maxPage = $this->m_movie->maxPage(10);
        $content['endPage'] = $maxPage;
        $content['pageArr'] = pageArray(2, $maxPage , $page);
        $content['currPage'] = $page;
        $this->load->view('movie/v_mostviews', $content);
    }


    /**
     * @param  $id
     * @param  $page
     * @return void
     */
    function search($id, $page){
        //import model
        $this->load->model('movie/m_movie');
        $this->load->helper('myurl');
        $value = $this->input->get_post('value');

        $maxPage = 0;
        $offset = ($page - 1) * 15;

        //xu ly value
        if($id == 0){
            $content['movieArray'] = $this->m_movie->searchByName($value, $offset, 15, $maxPage);
        }
        else
            $content['movieArray'] = $this->m_movie->searchByCategory($id, $value, $offset, 15, $maxPage);
        //load phan trang

        //page
        $this->load->helper('pagination');
        $content['endPage'] = $maxPage; //page end = max page
        $content['pageArr'] = pageArray(2, $maxPage , $page);
        $content['currPage'] = $page;
        
        $content['value'] = $value; //search val
        //$content['maxPage'] = $maxPage;
        //$content['page1'] = $page;

        $this->load->view('movie/v_search_content', $content);
    }

    /**
     * @param  $id
     * @param  $page
     * @return void
     */
    function searchCategory($id, $page){
        $maxPage = 0;
        $offset = ($page - 1) * 15;
        
        $this->load->model('movie/m_movie');
        $this->load->helper('myurl');
        $data = array();
        $data['movieArray'] = $this->m_movie->searchByCategory($id, '', $offset, 15, $maxPage);
        $data['value'] = '';
        //$data['maxPage'] = $maxPage;
        $data['flag'] = 'category';
        $data['catID'] = $id;
        //$data['page1'] = $page;
        //page
        $this->load->helper('pagination');
        $data['endPage'] = $maxPage; //page end = max page
        $data['pageArr'] = pageArray(2, $maxPage , $page);
        $data['currPage'] = $page;
        $this->load->view('movie/v_search_content', $data);
    }

    function searchActor($catID, $page){
        $maxPage = 0;
        $offset = ($page - 1) * 15;
        $value = $this->input->get_post('value');
        $this->load->model('movie/m_movie');

        $data = array();
        $data['movieArray'] = $this->m_movie->searchByActors($catID, $value, $offset, 15, $maxPage);
        $data['value'] = $value;
        //$data['maxPage'] = $maxPage;
        $data['flag'] = 'actor';
        $data['catID'] = $catID;
        //$data['page1'] = $page;
        $this->load->helper('myurl');
        $this->load->helper('pagination');
        $data['endPage'] = $maxPage; //page end = max page
        $data['pageArr'] = pageArray(2, $maxPage , $page);
        $data['currPage'] = $page;
        
        $this->load->view('movie/v_search_content', $data);
    }

    function _differentMovies($movieID){
        $this->load->model('movie/m_movie');
        $this->load->helper('myurl');
        return $this->m_movie->getDifferentMovies($movieID);
        //$data['movieList'] = $this->m_movie->getDifferentMovies($movieID);
        //return $this->view('v_different', $data, true);
    }
    /**
     * layout here
     */

    function _slideShow(){
        $this->load->model('movie/m_choice');
        $this->load->helper('myurl');
        $slideshow['choiceList'] = $this->m_choice->getChoiceMoviesList();
        $this->load->view('movie/v_slideshow', $slideshow);
    }

    function _footer(){
        $this->load->model('m_hitcounter');
        $data['hitCounter'] = $this->m_hitcounter->hitCounter();
        $data['online'] = $this->m_hitcounter->online();
        $this->load->view('movie/v_footer', $data);
    }

    function _header($tile, $meta){
        $data['meta'] = $meta;
        $data['title'] = $tile;
        $this->load->view('movie/v_header', $data);
    }

    function _leftMenu(){
        //lay danh sach catagory
        $this->load->model(array('movie/m_category', 'movie/m_actor'));
        $cat['categoryList'] = $this->m_category->getCategoryList();
        $cat['actorList'] = $this->m_actor->getActorList();
        $this->load->view('movie/v_leftmenu', $cat);
    }

    /*-----end layout---*/
}
?>
